{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## JSSP FJS Example\n",
    "\n",
    "This notebook demonstrates how to use parallel tabu search for a flexible job shop problem (fjs).\n",
    "\n",
    "Example fjs problem instances can be found [here](https://github.com/mcfadd/Job_Shop_Schedule_Problem/tree/master/data/fjs_data)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from JSSP import FJSData\n",
    "\n",
    "# initialize data\n",
    "fjs_instance_file = '../data/fjs_data/Barnes/Barnes_seti5x.fjs'\n",
    "fjs_instance_data = FJSData(fjs_instance_file)\n",
    "\n",
    "# uncomment this to print the data that was read in\n",
    "# print(fjs_instance_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running benchmark of TS\n",
      "Parameters:\n",
      "stopping_condition = 500\n",
      "time_condition = False\n",
      "num_solutions_per_process = 1\n",
      "num_processes = 4\n",
      "tabu_list_size = 15\n",
      "neighborhood_size = 200\n",
      "neighborhood_wait = 0.1\n",
      "probability_change_machine = 0.8\n",
      "reset_threshold = 100\n",
      "\n",
      "Initial Solution's makespans:\n",
      "[2516, 2251, 2621, 2640]\n",
      "\n",
      "child TS process started. pid = 8977\n",
      "child TS process started. pid = 8978\n",
      "child TS process started. pid = 8979\n",
      "child TS process started. pid = 8980\n",
      "child TS process finished. pid = 8977\n",
      "child TS process finished. pid = 8978\n",
      "child TS process finished. pid = 8979\n",
      "child TS process finished. pid = 8980\n",
      "\n",
      "Solution\n",
      "makespan = 1340.0\n",
      "machine_makespans = [1237.0, 1340.0, 1159.0, 1312.0, 1302.0, 1142.0, 1124.0, 1243.0, 1145.0, 1307.0, 1243.0, 1334.0, 1147.0, 1135.0, 1238.0, 1220.0]\n",
      "operation_list =\n",
      "[[ 2  0  0 14]\n",
      " [10  0  0  2]\n",
      " [ 7  0  0  3]\n",
      " [ 6  0  0  9]\n",
      " [12  0  0  0]\n",
      " [ 3  0  0  1]\n",
      " [ 0  0  0  9]\n",
      " [ 8  0  0  3]\n",
      " [ 3  1  1  6]\n",
      " [14  0  0  1]\n",
      " [ 4  0  0 12]\n",
      " [12  1  1 13]\n",
      " [ 7  1  1  7]\n",
      " [14  1  1 10]\n",
      " [ 1  0  0  0]\n",
      " [ 7  2  2  4]\n",
      " [ 0  1  1 10]\n",
      " [14  2  2  6]\n",
      " [11  0  0 10]\n",
      " [ 6  1  1  7]\n",
      " [14  3  3 12]\n",
      " [ 4  1  1  5]\n",
      " [ 6  2  2  6]\n",
      " [13  0  0 10]\n",
      " [ 6  3  3 14]\n",
      " [11  1  1 15]\n",
      " [ 8  1  1  8]\n",
      " [ 2  1  1  3]\n",
      " [ 3  2  2  7]\n",
      " [ 7  3  3  8]\n",
      " [ 1  1  1  1]\n",
      " [ 7  4  4  5]\n",
      " [10  1  1  0]\n",
      " [ 5  0  0  1]\n",
      " [12  2  2  3]\n",
      " [ 7  5  5  2]\n",
      " [ 6  4  4  3]\n",
      " [ 9  0  0  0]\n",
      " [ 9  1  1  5]\n",
      " [ 4  2  2  9]\n",
      " [ 0  2  2  4]\n",
      " [ 3  3  3  4]\n",
      " [ 0  3  3 12]\n",
      " [14  4  4  4]\n",
      " [ 2  2  2  1]\n",
      " [ 1  2  2 11]\n",
      " [ 8  2  2  6]\n",
      " [ 8  3  3  9]\n",
      " [11  2  2  4]\n",
      " [ 5  1  1  5]\n",
      " [13  1  1 12]\n",
      " [ 6  5  5 13]\n",
      " [ 0  4  4  2]\n",
      " [ 8  4  4 15]\n",
      " [14  5  5  8]\n",
      " [11  3  3  9]\n",
      " [ 8  5  5  1]\n",
      " [ 8  6  6  5]\n",
      " [ 9  2  2  7]\n",
      " [12  3  3  6]\n",
      " [ 7  6  6 15]\n",
      " [ 5  2  2  2]\n",
      " [ 6  6  6  2]\n",
      " [ 3  4  4 14]\n",
      " [ 7  7  7 12]\n",
      " [14  6  6  3]\n",
      " [10  2  2  6]\n",
      " [ 3  5  5 10]\n",
      " [ 4  3  3  4]\n",
      " [ 9  3  3  4]\n",
      " [11  4  4  3]\n",
      " [ 3  6  6  9]\n",
      " [ 0  5  5 15]\n",
      " [ 0  6  6  5]\n",
      " [14  7  7  7]\n",
      " [ 0  7  7  8]\n",
      " [14  8  8 13]\n",
      " [ 9  4  4 10]\n",
      " [ 4  4  4 15]\n",
      " [ 2  3  3 12]\n",
      " [ 2  4  4  6]\n",
      " [ 5  3  3 13]\n",
      " [13  2  2 13]\n",
      " [ 7  8  8 11]\n",
      " [10  3  3  4]\n",
      " [ 5  4  4  4]\n",
      " [ 8  7  7  4]\n",
      " [ 4  5  5 13]\n",
      " [13  3  3  4]\n",
      " [ 0  8  8  6]\n",
      " [14  9  9 11]\n",
      " [ 4  6  6  0]\n",
      " [ 8  8  8 13]\n",
      " [10  4  4  3]\n",
      " [ 7  9  9  0]\n",
      " [12  4  4  1]\n",
      " [ 4  7  7  8]\n",
      " [14 10 10  5]\n",
      " [ 8  9  9  7]\n",
      " [ 2  5  5  5]\n",
      " [ 7 10 10 13]\n",
      " [ 0  9  9  3]\n",
      " [ 2  6  6  8]\n",
      " [ 1  3  3  2]\n",
      " [ 6  7  7  4]\n",
      " [11  5  5  1]\n",
      " [14 11 11  9]\n",
      " [11  6  6 12]\n",
      " [ 6  8  8 12]\n",
      " [10  5  5  5]\n",
      " [ 6  9  9  8]\n",
      " [12  5  5 14]\n",
      " [13  4  4  1]\n",
      " [12  6  6 11]\n",
      " [ 8 10 10 11]\n",
      " [ 4  8  8 11]\n",
      " [ 5  5  5 15]\n",
      " [ 5  6  6  6]\n",
      " [14 12 12  2]\n",
      " [ 3  7  7  5]\n",
      " [ 1  4  4  4]\n",
      " [ 5  7  7  7]\n",
      " [12  7  7  4]\n",
      " [ 2  7  7 11]\n",
      " [11  7  7 13]\n",
      " [ 3  8  8 11]\n",
      " [ 8 11 11 12]\n",
      " [ 1  5  5  9]\n",
      " [12  8  8 10]\n",
      " [10  6  6 12]\n",
      " [ 8 12 12 10]\n",
      " [ 8 13 13  2]\n",
      " [ 5  8  8  9]\n",
      " [ 8 14 14  0]\n",
      " [12  9  9  9]\n",
      " [10  7  7  9]\n",
      " [ 0 10 10  1]\n",
      " [ 5  9  9 10]\n",
      " [14 13 13 14]\n",
      " [ 1  6  6 15]\n",
      " [ 4  9  9  1]\n",
      " [ 1  7  7  8]\n",
      " [ 4 10 10  2]\n",
      " [ 0 11 11  7]\n",
      " [ 6 10 10  1]\n",
      " [ 5 10 10 11]\n",
      " [ 2  8  8  7]\n",
      " [ 3  9  9  2]\n",
      " [ 6 11 11 10]\n",
      " [13  5  5  3]\n",
      " [10  8  8 14]\n",
      " [11  8  8  6]\n",
      " [ 5 11 11  0]\n",
      " [ 1  8  8 13]\n",
      " [ 5 12 12  3]\n",
      " [13  6  6  8]\n",
      " [ 0 12 12 13]\n",
      " [ 0 13 13 11]\n",
      " [ 9  5  5 12]\n",
      " [12 10 10  5]\n",
      " [ 1  9  9 12]\n",
      " [ 9  6  6  1]\n",
      " [13  7  7  5]\n",
      " [13  8  8  9]\n",
      " [12 11 11  8]\n",
      " [ 1 10 10  6]\n",
      " [ 9  7  7 13]\n",
      " [11  9  9  8]\n",
      " [13  9  9  0]\n",
      " [ 3 10 10 13]\n",
      " [12 12 12  7]\n",
      " [ 4 11 11  7]\n",
      " [ 6 12 12  0]\n",
      " [ 2  9  9 10]\n",
      " [ 9  8  8  6]\n",
      " [12 13 13  2]\n",
      " [14 14 14  0]\n",
      " [12 14 14 12]\n",
      " [13 10 10  6]\n",
      " [ 7 11 11 10]\n",
      " [ 4 12 12  6]\n",
      " [ 5 13 13 12]\n",
      " [ 9  9  9  8]\n",
      " [11 10 10 11]\n",
      " [ 3 11 11  8]\n",
      " [ 9 10 10 11]\n",
      " [ 2 10 10  2]\n",
      " [ 1 11 11  3]\n",
      " [11 11 11  7]\n",
      " [ 3 12 12  3]\n",
      " [ 6 13 13  5]\n",
      " [11 12 12  5]\n",
      " [ 0 14 14  0]\n",
      " [ 1 12 12 10]\n",
      " [ 6 14 14 11]\n",
      " [10  9  9 13]\n",
      " [11 13 13  2]\n",
      " [ 2 11 11 13]\n",
      " [ 4 13 13 10]\n",
      " [ 9 11 11  9]\n",
      " [13 11 11  7]\n",
      " [ 1 13 13  5]\n",
      " [11 14 14  0]\n",
      " [10 10 10  8]\n",
      " [10 11 11  7]\n",
      " [10 12 12 11]\n",
      " [ 2 12 12  0]\n",
      " [13 12 12  2]\n",
      " [10 13 13 10]\n",
      " [13 13 13 14]\n",
      " [ 9 12 12  2]\n",
      " [ 7 12 12  1]\n",
      " [10 14 14  1]\n",
      " [ 5 14 14  8]\n",
      " [ 3 13 13 12]\n",
      " [ 2 13 13  9]\n",
      " [ 7 13 13  6]\n",
      " [13 14 14 11]\n",
      " [ 1 14 14  7]\n",
      " [ 3 14 14  0]\n",
      " [ 4 14 14  3]\n",
      " [ 9 13 13 15]\n",
      " [ 9 14 14  3]\n",
      " [ 2 14 14  4]\n",
      " [ 7 14 14  9]]\n",
      "opening file:///home/mcfadd/Projects/Job_Shop_Schedule_Problem/examples/example_output/example_benchmark in browser\n"
     ]
    }
   ],
   "source": [
    "from JSSP.solver import Solver\n",
    "\n",
    "output_dir = './example_output'\n",
    "\n",
    "# optional\n",
    "output_results = True\n",
    "auto_open = True\n",
    "benchmark = True\n",
    "verbose = True\n",
    "\n",
    "# TS parameters\n",
    "iterations = 500\n",
    "num_processes = 4\n",
    "num_solutions_per_process = 1\n",
    "tabu_list_size = 15\n",
    "neighborhood_size = 200\n",
    "neighborhood_wait = 0.1\n",
    "probability_change_machine = 0.8\n",
    "reset_threshold = 100\n",
    "\n",
    "# create solver\n",
    "solver = Solver(fjs_instance_data)\n",
    "\n",
    "# run TS\n",
    "solution = solver.tabu_search_iter(iterations=iterations,\n",
    "                                   num_processes=num_processes,\n",
    "                                   num_solutions_per_process=num_solutions_per_process,\n",
    "                                   tabu_list_size=tabu_list_size,\n",
    "                                   neighborhood_size=neighborhood_size,\n",
    "                                   neighborhood_wait=neighborhood_wait,\n",
    "                                   probability_change_machine=probability_change_machine,\n",
    "                                   reset_threshold=reset_threshold,\n",
    "                                   benchmark=benchmark,\n",
    "                                   verbose=verbose\n",
    "                                   )\n",
    "\n",
    "print('\\nSolution')\n",
    "print(solution)\n",
    "\n",
    "if output_results:\n",
    "    if benchmark:\n",
    "        solver.output_benchmark_results(output_dir, name='example_benchmark', auto_open=auto_open)\n",
    "    else:\n",
    "        solution.create_schedule_xlsx_file(output_dir)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
